package com.cc2019.buke.home.frag;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cc2019.buke.R;
import com.cc2019.buke.home.adapter.HomeAdapter;
import com.cc2019.buke.home.entry.HomeDataBean;
import com.cc2019.buke.home.entry.Product2;
import com.ejlchina.okhttps.OkHttps;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.List;


public class HomeFragment extends Fragment {


    private RecyclerView homeRecycleView;
    private Context context;
    private static final int REFRESH = 0;//请求3个板块的数据
    private static final int LOADING = 1;//加载更多热门商品
    HomeAdapter homeAdapter;
    int pageNow = 2;


    @Override//fragment界面被创建时使用
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        //找到RecycleView
        homeRecycleView = view.findViewById(R.id.home_recycle);
        context = this.getContext();
        //当fragment页面被加载时
        RefreshLayout refreshLayout = (RefreshLayout)view.findViewById(R.id.refreshLayout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                getData(REFRESH);
                refreshlayout.finishRefresh(true);//传入false表示刷新失败
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                getData(LOADING);
                refreshlayout.finishLoadMore(true);//传入false表示加载失败
            }
        });

        return view;

    }
    public void onActivityCreated(@Nullable Bundle savedInstanceState){
        super.onActivityCreated(savedInstanceState);
        //请求到数据，设置适配器
        Context context = this.getContext();
        //需要请求数据,请求所有板块数据
        getData(REFRESH);
        //设置Adapter
        //设置布局样式为grilLayout
        GridLayoutManager gridLayoutManager = new GridLayoutManager(context,1);
        homeRecycleView.setLayoutManager(gridLayoutManager);
    }
    public void getData(int flag){
        if (flag==REFRESH){

            new Thread(new Runnable() {
                @Override
                public void run() {
                    //请求所有板块的数据，同步请求
                    //请求秒杀板块数据
                    List<Product2> sg_pros = OkHttps.sync("http://124.221.67.36:9998/android/findSome").bind(context).get().getBody().toList(Product2.class);
                    System.out.println(sg_pros);
                    //请求推荐板块数据
                    List<Product2> tj_pros = OkHttps.sync("http://124.221.67.36:9998/android/findSome").bind(context).get().getBody().toList(Product2.class);
                    //请求热销板块数据
                    List<Product2> rx_pros = OkHttps.sync("http://124.221.67.36:9998/android/findByPage?pageNow=1&pageSize=20").bind(context).get().getBody().toList(Product2.class);
                    //封装一个HomeDataBean
                    HomeDataBean homeDataBean = new HomeDataBean();
                    homeDataBean.setSg_pros(sg_pros);
                    homeDataBean.setRx_pros(rx_pros);
                    homeDataBean.setTj_pros(tj_pros);
                    //当执行到这里时，三个板块的数据，都请求成功
                    //发消息给主线程，数据有了，修改ui
                    Message message = Message.obtain();
                    message.what = REFRESH;
                    message.obj = homeDataBean;
                    //发送消息给分线程handler处理
                    handler.sendMessage(message);
                }
            }).start();
        }else if (flag == LOADING){
            //使用异步请求--请求第n页的数据
//            OkHttps.async("http://124.221.67.36:9998/android/findByPage")
//                    .bind(context)
//                    .addUrlPara("pageNow",2)
//                    .addUrlPara("pageSize",20)
//                    .setOnResList(Product2.class,rx_pros ->{
//                        //接收结果，通知homeAdapter,通知rxAdapter
//                        homeAdapter.getNotice(rx_pros);
//            }).get();
//            pageNow ++;
        }
    }
    private Handler handler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(@NonNull Message msg) {
            //接收到的数据
            super.handleMessage(msg);
            switch (msg.what){
                case REFRESH:
                    HomeDataBean homeData = (HomeDataBean)msg.obj;
                    homeAdapter = new HomeAdapter(context,homeData);
                    homeRecycleView.setAdapter(homeAdapter);
            }
        }
    };
}